package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	maze1
*/
public class maze1 {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("maze1.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"maze1.out")));
		new maze1().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private int W;
	private int H;
	private boolean[][] h;
	private boolean[][]	v;
	
	private ArrayDeque<Point> queue;
	private ArrayList<Point> list;
	private boolean[][] visited;
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		int[] a = read(f, 2);
		W = a[0];
		H = a[1];
		h = new boolean[H][W+1];
		v = new boolean[H+1][W];
		
		for (int i=0; i<=2*H; i++) {
			String s = f.readLine();
			
			if (i%2 == 0) {			// vertical
				for (int j=0; j<W; j++) {
					v[i/2][j] = s.charAt(2*j+1)=='-'? true : false;
				}
			}
			else {					// horizontal
				for (int j=0; j<=W; j++) {
					h[i/2][j] = s.charAt(2*j)=='|'? true : false; 
				}
			}
		}
		
		// find two exits
		queue = new ArrayDeque<Point>();
		visited = new boolean[H][W];
		
		for (int i=0; i<H; i++) {
			if (!h[i][0]) {
				queue.add(new Point(i, 0));
				visited[i][0] = true;
			}
			if (!h[i][W]) {
				queue.add(new Point(i, W-1));
				visited[i][W-1] = true;
			}
		}
		
		for (int i=0; i<W; i++) {
			if (!v[0][i]) {
				queue.add(new Point(0, i));
				visited[0][i] = true;
			}
			if (!v[H][i]) {
				queue.add(new Point(H-1, i));
				visited[H-1][i] = true;
			}
		}
		
		int max = 0;
		list = new ArrayList<Point>();
		while (!queue.isEmpty()) {
			list.clear();
			while (!queue.isEmpty()) {
				Point p = queue.remove();
				if (p.row > 0 && !v[p.row][p.col]) move(p.row-1, p.col);
				if (p.row < H-1 && !v[p.row+1][p.col]) move(p.row+1, p.col);
				if (p.col > 0 && !h[p.row][p.col]) move(p.row, p.col-1);
				if (p.col < W-1 && !h[p.row][p.col+1]) move(p.row, p.col+1);
			}
			
			max++;
			queue.addAll(list);
		}
		
		out.println(max);
	}
	
	private void move(int row, int col) {
		if (visited[row][col]) return;
		visited[row][col] = true;
		list.add(new Point(row, col));
	}
	
	private class Point {
		int row;
		int col;
		public Point(int row, int col) {
			this.row = row;
			this.col = col;
		}
	}
	
	public int read(BufferedReader f) throws IOException {
		return Integer.parseInt(f.readLine());
	}
	
	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split(" ");
		int[] a = new int[N];
		for (int i=0; i<N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
